﻿<%@ WebHandler Language="C#" Class="contactsMgr" %>

using System;
using System.Web;
using System.Text;
using System.Data;
using System.Collections;
using System.Collections.Generic;
using Chenxin.Lib.Utility;

public class contactsMgr  : AjaxHandlerBase
{

    public contactsMgr()
    {
        //转化为黑名单用户
        htTypePermit.Add("toblacklist", Constants.Permit_TransToBlackList);
    }
    override public void RequestProcess(HttpContext context)
    {
        context.Response.Buffer = true;
        context.Response.ExpiresAbsolute = DateTime.Now.AddDays(-1);
        context.Response.AddHeader("pragma", "no-cache");
        context.Response.AddHeader("cache-control", "");
        context.Response.CacheControl = "no-cache";
        context.Response.ContentType = "application/json";

        string strDealType = context.Request.Params["op"];

        string strReuslt = "";
                
        switch (strDealType)
        {
            case "GetDepart"://根据Key获取数据字典中相应的字典项目
                strReuslt = GetDepart(context);
                break;
            case "GetPersonnelCondition":
                strReuslt = GetPersonnelCondition(context);
                break;
            case "GetPersonalCondition":
                strReuslt = GetPersonalCondition(context);
                break;
            case "SearchPersonnel"://根据Key获取数据字典中相应的字典项目
                strReuslt = SearchPersonnel(context);
                break;
            case "SearchPersonal"://根据Key获取数据字典中相应的字典项目
                strReuslt = SearchPersonal(context);
                break;
            case "Save"://根据Key获取数据字典中相应的字典项目
                strReuslt = SaveContact(context);
                break;
            case "Del"://根据Key获取数据字典中相应的字典项目
                strReuslt = DelContact(context);
                break;
            case "ToBlackList"://根据Key获取数据字典中相应的字典项目
                strReuslt = AddToBlackList(context);
                break;
            default:
                break;
        }

        context.Response.Write(strReuslt);
    }

    //*******************************************
    //功能概要       ：获得条件列表
    //参数説明       ：context 请求上下文
    //返回值説明     ：回传Html字符串
    //功能詳細       ：获得条件列表
    //作成者         ：he.jifeng 
    //作成日         ：2010-04-14 
    //*******************************************
    private string GetPersonnelCondition(HttpContext context)
    {
        string strRet;
        try
        {            
            StringBuilder strHtml = new StringBuilder();

            string strQuery = context.Request.Params["query"];
            
            Chenxin.Lib.Passport.BLL.Hr_UserInfo bllUserInfo = new Chenxin.Lib.Passport.BLL.Hr_UserInfo();

            System.Collections.ArrayList lsUsersDeparts = bllUserInfo.GetUsersDeparts(strQuery);

            List<Chenxin.Lib.Passport.Entity.Hr_UserInfo> lsUserInfo = (List<Chenxin.Lib.Passport.Entity.Hr_UserInfo>)lsUsersDeparts[0];
            
            if (lsUserInfo.Count > 0)
            {
                strHtml.Append("<dt>人员</dt>");

                foreach (Chenxin.Lib.Passport.Entity.Hr_UserInfo lsUser in lsUserInfo)
                {

                    string strShowNm = lsUser.UserName + "(" + lsUser.UserCode + ")";
                    strHtml.Append("<dd>");
                    strHtml.Append(" <input type=hidden name='hidType' value='UserInfo' />");
                    strHtml.Append(" <input type=hidden name='hidId' value='" + lsUser.UserId.ToString() + "' />");
                    strHtml.Append(" <input type=hidden name='hidNm' value='" + strShowNm + "' />");
                    strHtml.Append(" <a class='clsAUser' href='#'>" + strShowNm + "</a>");
                    strHtml.Append("</dd>");
                }
            }
            
            strRet = retExecResult(Constants.ExecResult.Succ, "", strHtml.ToString());
            return strRet;
        }
        catch (Exception ex)
        {
            strRet = retExecResult(Constants.ExecResult.Error, ex.Message, "");
            return strRet;
        }
    }

    //*******************************************
    //功能概要       ：获得条件列表
    //参数説明       ：context 请求上下文
    //返回值説明     ：回传Html字符串
    //功能詳細       ：获得条件列表
    //作成者         ：he.jifeng 
    //作成日         ：2010-04-14 
    //*******************************************
    private string GetPersonalCondition(HttpContext context)
    {
        string strRet;
        try
        {
            StringBuilder strHtml = new StringBuilder();

            string strQuery = context.Request.Params["query"];

            Chenxin.Lib.Hr.BLL.Hr_Contacts bllContacts = new Chenxin.Lib.Hr.BLL.Hr_Contacts();

            List<Chenxin.Lib.Hr.Entity.Hr_Contacts> lsUserInfo = bllContacts.GetListByQuery(strQuery);

            if (lsUserInfo.Count > 0)
            {
                strHtml.Append("<dt>人员</dt>");

                foreach (Chenxin.Lib.Hr.Entity.Hr_Contacts User in lsUserInfo)
                {

                    string strShowNm = User.UserName;
                    strHtml.Append("<dd>");
                    strHtml.Append(" <input type=hidden name='hidType' value='UserInfo' />");
                    strHtml.Append(" <input type=hidden name='hidId' value='" + User.RecordId.ToString() + "' />");
                    strHtml.Append(" <input type=hidden name='hidNm' value='" + strShowNm + "' />");
                    strHtml.Append(" <a class='clsAUser' href='#'>" + strShowNm + "</a>");
                    strHtml.Append("</dd>");
                }
            }
            
            strRet = retExecResult(Constants.ExecResult.Succ, "", strHtml.ToString());
            return strRet;
        }
        catch (Exception ex)
        {
            strRet = retExecResult(Constants.ExecResult.Error, ex.Message, "");
            return strRet;
        }
    }
    
    //*******************************************
    //功能概要       ：人事通讯录查询
    //参数説明       ：context 请求上下文
    //返回值説明     ：回传Html字符串
    //功能詳細       ：人事通讯录查询
    //作成者         ：he.jifeng 
    //作成日         ：2010-04-14 
    //*******************************************
    private string SearchPersonnel(HttpContext context)
    {
        //参数
        Hashtable htParams = new Hashtable();
        string strRet;

        if (!string.IsNullOrEmpty(context.Request["userid"]))
        {
            htParams.Add("UserId", context.Request["userid"]);
        }
        else if (!string.IsNullOrEmpty(context.Request["departid"]))
        {
            htParams.Add("DepartId", context.Request["departid"]);
        }
        else if (!string.IsNullOrEmpty(context.Request["areaid"]))
        {
            htParams.Add("AreaId", context.Request["areaid"]);
        }
        else if (!string.IsNullOrEmpty(context.Request["companyid"]))
        {
            htParams.Add("CompanyId", context.Request["companyid"]);
        }
        else if (!string.IsNullOrEmpty(context.Request["cityid"]))
        {
            htParams.Add("CityId", context.Request["cityid"]);
        }
            //默认检索条件
        else
        {
            htParams.Add("CompanyId", CurrentUserInfo.CompanyId.ToString());
        }
        
        //首字母
        if (!string.IsNullOrEmpty(context.Request.Params["Initial"]))
        {
            htParams.Add("Initial", context.Request.Params["Initial"]);
        }
        
        //页码        
        if (!string.IsNullOrEmpty(context.Request.Params["pageIndex"]))
        {
            htParams.Add("pageIndex", (int.Parse(context.Request.Params["pageIndex"]) + 1).ToString());
        }
        //页显示行数
        if (!string.IsNullOrEmpty(context.Request.Params["pageSize"]))
        {
            htParams.Add("pageSize", (context.Request.Params["pageSize"]));
        }

        //权限判断
        //查看所有权限
        if (CurrentUserInfo.ContainsPermit(Constants.Permit_ViewAllContacts))
        {
            htParams.Add("Permit", Constants.Permit_ViewAllContacts);
        }
        //查看城市权限
        else if (CurrentUserInfo.ContainsPermit(Constants.Permit_ViewCityContacts))
        {
            htParams.Add("Permit", Constants.Permit_ViewCityContacts);
            htParams.Add("CurCityId", CurrentUserInfo.CityId);
        }
        //查看公司权限
        else if (CurrentUserInfo.ContainsPermit(Constants.Permit_ViewCompanyContacts))
        {
            htParams.Add("Permit", Constants.Permit_ViewCompanyContacts);
            htParams.Add("CurCompanyId", CurrentUserInfo.CompanyId);
        }
        //查看部门权限
        else if (CurrentUserInfo.ContainsPermit(Constants.Permit_ViewDepartContacts))
        {
            htParams.Add("Permit", Constants.Permit_ViewDepartContacts);
            htParams.Add("CurUserId", CurrentUserInfo.UserId);
        }
        //查看自己权限    
        else
        {
            htParams.Add("CurUserId", CurrentUserInfo.UserId);
        }
        
        try
        {
            Chenxin.Lib.Passport.BLL.Hr_UserInfo bllUserInfo = new Chenxin.Lib.Passport.BLL.Hr_UserInfo();

            DataSet ds = bllUserInfo.GetPersonnelListByConditions(htParams);
            int CntOfPersonnel = Convert.ToInt32(ds.Tables[1].Rows[0][0]);
            Constants.Pagination stuPg = crtHtml(ds.Tables[0], CntOfPersonnel);
            strRet = retExecResult(Constants.ExecResult.Succ, "", stuPg);
            return strRet;
        }
        catch (Exception ex)
        {
            strRet = retExecResult(Constants.ExecResult.Error, ex.Message, "");
            return strRet;
        }
    }
    //*******************************************
    //功能概要       ：个人通讯录查询
    //参数説明       ：context 请求上下文
    //返回值説明     ：回传Html字符串
    //功能詳細       ：个人通讯录查询
    //作成者         ：he.jifeng 
    //作成日         ：2010-04-14 
    //*******************************************
    private string SearchPersonal(HttpContext context)
    {
        //参数
        Hashtable htParams = new Hashtable();
        string strRet;
        
        //标识ID
        if (!string.IsNullOrEmpty(context.Request.Params["RecordId"]))
        {
            htParams.Add("RecordId", context.Request.Params["RecordId"]);
        }
        
        //页码        
        if (!string.IsNullOrEmpty(context.Request.Params["pageIndex"]))
        {
            htParams.Add("pageIndex", (int.Parse(context.Request.Params["pageIndex"]) + 1).ToString());
        }
        //页显示行数
        if (!string.IsNullOrEmpty(context.Request.Params["pageSize"]))
        {
            htParams.Add("pageSize", (context.Request.Params["pageSize"]));
        }

        //首字母
        if (!string.IsNullOrEmpty(context.Request.Params["Initial"]))
        {
            htParams.Add("Initial", context.Request.Params["Initial"]);
        }
        
        try
        {
            Chenxin.Lib.Hr.BLL.Hr_Contacts bllContacts = new Chenxin.Lib.Hr.BLL.Hr_Contacts();

            int CntOfPersonal = bllContacts.GetCntOfContacts(htParams);
            DataSet ds = bllContacts.GetListByConditions(htParams);
            Constants.Pagination stuPg = crtHtml(ds.Tables[0], CntOfPersonal);
            strRet = retExecResult(Constants.ExecResult.Succ, "", stuPg);
            return strRet;
        }
        catch (Exception ex)
        {
            strRet = retExecResult(Constants.ExecResult.Error, ex.Message, "");
            return strRet;
        }
    }

    //*******************************************
    //功能概要       ：转化通讯录显示
    //参数説明       ：dt 通讯录列表
    //               ：CntOfTotal 总记录数
    //返回值説明     ：回传Html字符串
    //功能詳細       ：转化通讯录显示
    //作成者         ：he.jifeng 
    //作成日         ：2010-04-14 
    //*******************************************
    private Constants.Pagination crtHtml(DataTable dt, int CntOfTotal)
    {
        StringBuilder strHtml = new StringBuilder();
        Constants.Pagination stuPg = new Constants.Pagination();

        for (int idxRow = 0; idxRow < dt.Rows.Count; idxRow++)
        {
            strHtml.Append("<div class=\"outshadow fl\">");
            strHtml.Append("<div class=\"conpsonbox\">");
            if (dt.Columns[0].ColumnName == "UserId")
            {
                strHtml.Append(string.Format("<h1>{0}<a href=\"#\" class=\"fr clsBlackList\">黑名单</a></h1>", dt.Rows[idxRow][1]));
            }
            else
            {
                strHtml.Append(string.Format("<h1>{0}<a href=\"#\" class=\"fr clsEdit\">编辑</a><a href=\"#\" class=\"fr clsDel\">删除</a><a href=\"#\" class=\"fr clsBlackList\">黑名单</a></h1>", dt.Rows[idxRow][1]));
            }
            strHtml.Append(string.Format("<p>姓名：{0}</p>", dt.Rows[idxRow][1]));
            strHtml.Append(string.Format("<p>工号：{0}</p>", dt.Rows[idxRow][2]));
            strHtml.Append(string.Format("<p>部门：{0}</p>", dt.Rows[idxRow][3]));
            strHtml.Append(string.Format("<p>职位：{0}</p>", dt.Rows[idxRow][4]));
            strHtml.Append(string.Format("<p>电话：{0}</p>", dt.Rows[idxRow][5]));
            //strHtml.Append(string.Format("<p>住宅：{0}</p>", dt.Rows[idxRow][6]));
            strHtml.Append(string.Format("<p>邮箱：{0}</p>", dt.Rows[idxRow][7]));
            strHtml.Append(string.Format("<input type=\"hidden\" name=\"UserId\" value=\"{0}\">", dt.Rows[idxRow][0]));
            strHtml.Append("</div>");
            strHtml.Append("</div>");
        }
        stuPg.count = CntOfTotal;
        stuPg.data = strHtml.ToString();

        return stuPg;
    }

    //*******************************************
    //功能概要       ：保存联系人
    //参数説明       ：context 请求上下文
    //返回值説明     ：回传Html字符串
    //功能詳細       ：保存联系人
    //作成者         ：he.jifeng 
    //作成日         ：2010-04-14 
    //*******************************************
    private string SaveContact(HttpContext context)
    {
        string strRet;
        string strMsg;
        
        try
        {
            Chenxin.Lib.Hr.Entity.Hr_Contacts entContacts =
            (Chenxin.Lib.Hr.Entity.Hr_Contacts)Chenxin.Lib.Utility.JsonHelp.Convert2Entity(context.Request["model"], typeof(Chenxin.Lib.Hr.Entity.Hr_Contacts));

            Chenxin.Lib.Hr.BLL.Hr_Contacts bllContacts = new Chenxin.Lib.Hr.BLL.Hr_Contacts();

            if (Guid.Empty.Equals(entContacts.RecordId))
            {
                entContacts.RecordId = Guid.NewGuid();
                entContacts.UpdateTime = DateTime.Now;
                entContacts.OwnerId = CurrentUserInfo.UserId;
                entContacts.Initial = InitialHelper.getInitial(entContacts.UserName.Substring(0, 1));
                bllContacts.Add(entContacts);
                strMsg = "添加成功";

                //添加用户操作日志 2010-5-12 王玮 Add
                AddLog("" + CurrentUserInfo.UserName + "添加了一个联系人:姓名[" + entContacts.UserName + "]",
                        OperationType.OperationTypeComboList()["OprType_ContactAdd"].ToString()); 
            }
            else
            {
                entContacts.UpdateTime = DateTime.Now;
                entContacts.OwnerId = CurrentUserInfo.UserId;
                entContacts.Initial = InitialHelper.getInitial(entContacts.UserName.Substring(0, 1));
                bllContacts.Update(entContacts);
                strMsg = "更新成功";

                //添加用户操作日志 2010-5-12 王玮 Add
                AddLog("" + CurrentUserInfo.UserName + "编辑了一个联系人:姓名[" + entContacts.UserName + "]",
                        OperationType.OperationTypeComboList()["OprType_ContactEdit"].ToString()); 
            }
            strRet = retExecResult(Constants.ExecResult.Succ, strMsg, "");
            return strRet;
        }
        catch (Exception ex)
        {
            strRet = retExecResult(Constants.ExecResult.Error, ex.Message, "");
            return strRet;
        }
    }

    //*******************************************
    //功能概要       ：删除联系人
    //参数説明       ：context 请求上下文
    //返回值説明     ：回传Html字符串
    //功能詳細       ：删除联系人
    //作成者         ：he.jifeng 
    //作成日         ：2010-04-14 
    //*******************************************
    private string DelContact(HttpContext context)
    {
        string strRet;
        try
        {
            string RecordId = context.Request.Params["RecordId"];
            Chenxin.Lib.Hr.Entity.Hr_Contacts clhehc = Chenxin.Lib.Hr.DAL.Hr_Contacts.GetModel(new Guid(RecordId));
            //删除记录
            Chenxin.Lib.Hr.DAL.Hr_Contacts.Delete(new Guid(RecordId));

            //添加用户操作日志 2010-5-12 王玮 Add
            AddLog("" + CurrentUserInfo.UserName + "删除了一个联系人:姓名[" + clhehc.UserName + "]",
                    OperationType.OperationTypeComboList()["OprType_ContactDel"].ToString()); 
            
            strRet = retExecResult(Constants.ExecResult.Succ, "删除成功", "");
            return strRet;
        }
        catch (Exception ex)
        {
            strRet = retExecResult(Constants.ExecResult.Error, ex.Message, "");
            return strRet;
        }
    }

    //*******************************************
    //功能概要       ：转化为黑名单
    //参数説明       ：context 请求上下文
    //返回值説明     ：回传Html字符串
    //功能詳細       ：转化为黑名单
    //作成者         ：he.jifeng 
    //作成日         ：2010-04-14 
    //*******************************************
    private string AddToBlackList(HttpContext context)
    {
        string strRet;
        try
        {
            string RecordId = context.Request.Params["RecordId"];
            string Type = context.Request.Params["Type"];

            Chenxin.Lib.Hr.Entity.Hr_BlackList entBlackList = new Chenxin.Lib.Hr.Entity.Hr_BlackList();
            //黑名单业务类
            Chenxin.Lib.Hr.BLL.Hr_BlackList bllBlackList = new Chenxin.Lib.Hr.BLL.Hr_BlackList();


            if (bllBlackList.GetModel(new Guid(RecordId)) != null)
            {
                strRet = retExecResult(Constants.ExecResult.Succ, "此联系人已在黑名单中", "");
                return strRet;
            }

            if ("Personnel".Equals(Type))
            {
                Chenxin.Lib.Passport.BLL.Hr_UserInfo bllUserInfo = new Chenxin.Lib.Passport.BLL.Hr_UserInfo();
                Chenxin.Lib.Passport.Entity.Hr_UserInfo entUserInfo = bllUserInfo.GetEntityById(new Guid(RecordId));

                entBlackList.RecordId = entUserInfo.UserId;
                entBlackList.UserName = entUserInfo.UserName;
                entBlackList.Tel = entUserInfo.HomeTel;
                entBlackList.Mobile = entUserInfo.Mobile;
                entBlackList.Email = entUserInfo.Email;
                entBlackList.Address = entUserInfo.Address;
                entBlackList.AddCompany = CurrentUserInfo.CompanyName;
                entBlackList.AddDepart = CurrentUserInfo.DepartName;
                entBlackList.AddUser = CurrentUserInfo.UserName;
                entBlackList.Level = 30;
                entBlackList.UpdateTime = DateTime.Now;
                entBlackList.Initial = InitialHelper.getInitial(entUserInfo.UserName.Substring(0, 1));
                
            }
            else
            {
                Chenxin.Lib.Hr.BLL.Hr_Contacts bllContacts = new Chenxin.Lib.Hr.BLL.Hr_Contacts();
                Chenxin.Lib.Hr.Entity.Hr_Contacts entContacts = bllContacts.GetModel(new Guid(RecordId));

                entBlackList.RecordId = entContacts.RecordId;
                entBlackList.UserName = entContacts.UserName;
                entBlackList.Tel = entContacts.Tel;
                entBlackList.Mobile = entContacts.Mobile;
                entBlackList.Email = entContacts.Email;
                entBlackList.AddCompany = CurrentUserInfo.CompanyName;
                entBlackList.AddDepart = CurrentUserInfo.DepartName;
                entBlackList.AddUser = CurrentUserInfo.UserName;
                entBlackList.Level = 30;
                entBlackList.UpdateTime = DateTime.Now;
                entBlackList.Initial = InitialHelper.getInitial(entContacts.UserName.Substring(0, 1));
            }

            if (Guid.Empty.Equals(entBlackList.RecordId))
            {
                strRet = retExecResult(Constants.ExecResult.Succ, "转换过程中数据已被改变，请刷新重新执行", "");
                return strRet;
            }
            Chenxin.Lib.Hr.DAL.Hr_BlackList.Add(entBlackList);
            
            //添加用户操作日志 2010-5-12 王玮 Add
            AddLog("" + CurrentUserInfo.UserName + "添加了一个黑名单:姓名[" + entBlackList.UserName + "]",
                    OperationType.OperationTypeComboList()["OprType_BlackListAdd"].ToString()); 
            
            strRet = retExecResult(Constants.ExecResult.Succ, "成功添加到黑名单", "");
            return strRet;
        }
        catch (Exception ex)
        {
            strRet = retExecResult(Constants.ExecResult.Error, ex.Message, "");
            return strRet;
        }
    }
    
    /// <summary>
    /// 
    /// </summary>
    /// <param name="strKey"></param>
    /// <returns></returns>
    //private string GetDataDicByKey(string strKey)
    private string GetDepart(HttpContext context)
    {
        DataSet dsDeparts = CurrentUserInfo.GetUserControledDeparts();

        //json数据
        string strJson = "{ txt:'请选择', val:''}";

        //循环列表
        for (int idxCnt = 0; idxCnt < dsDeparts.Tables[0].Rows.Count; idxCnt++)
        {
            strJson = strJson + ",{ txt: '";
            strJson = strJson + dsDeparts.Tables[0].Rows[idxCnt]["DepartName"].ToString();
            strJson = strJson + "', val: '";
            strJson = strJson + dsDeparts.Tables[0].Rows[idxCnt]["DepartId"].ToString();
            strJson = strJson + "' }";
        }

        strJson = "[" + strJson + "]";
        return strJson;
    }

}